package router

import (
	"fmt"
	"ginchat/common/middleware"
	"ginchat/service"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
)

func Router() *gin.Engine {
	router := gin.Default()

	router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

	// 静态资源
	router.Static("/asset", "asset/")
	router.LoadHTMLGlob("views/*")

	// 首页
	router.GET("/index", service.GetIndex)
	router.GET("/", service.GetIndex)

	router.GET("/ll", service.GetUserList)

	// 登录注册
	router.POST("/login", service.UserLogin)
	router.POST("/reg", service.UserRegister)

	// 发送消息
	message := router.Group("/message")
	message.Use()
	{
		message.GET("/send", service.SendMsg)
		message.GET("/chat", service.SendUserMsg)
		//message.POST("/get", service.MsgHandler)
	}

	// 用户板块
	user := router.Group("/user")
	user.Use(middleware.JwtToken())
	{
		user.GET("/user_list", service.GetUserList)
		user.POST("/add_user", service.CreateUser)
		user.POST("/del_user", service.DelUser)
		user.POST("/update", service.UpdateUser)
	}

	err := router.Run(viper.GetString("server.httpPort"))
	if err != nil {
		fmt.Println("gin启动失败", err)
		return nil
	}
	return router
}
